<template>
  <div class="app-container">
    <!-- 搜索筛选操作栏 -->
    <div class="filter-container clearfix">
      <el-date-picker
        v-model="listQuery.time"
        :clearable="false"
        class="filter-item"
        type="month"
        placeholder="选择月份"
        style="max-width: 100%"
        @change="datepickerChange"
      />
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        style="margin-left: 10px"
        @click="handleFilter"
      >
        搜索
      </el-button>
      <el-button
        v-waves
        class="filter-item"
        icon="iconfont qt-caozuo-xunhuan1"
        @click="clearFilter"
      >
        重置
      </el-button>
    </div>

    <!-- 表格 -->
    <el-table
      :key="tableKey"
      ref="table"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%"
    >
      <el-table-column label="日期" prop="created_at" align="center" />
      <el-table-column label="交易金额(元)" align="center" prop="offer_money" />
      <el-table-column align="center" label="平台佣金(元)" prop="commission" />
      <el-table-column align="center" label="提现金额(元)" prop="deposit" />
    </el-table>

    <!-- 分页 -->
    <div v-if="total > 0" style="text-align: right">
      <pagination
        :total="total"
        :page.sync="listQuery.page"
        :limit.sync="listQuery.per_page"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { getBusinessTotal } from '@/api/common'
import waves from '@/directive/waves' // waves directive
import Pagination from '@/components/Pagination' // secondary package based on el-pagination

export default {
  name: 'TotalBusiness',
  components: {
    Pagination
  },
  directives: {
    waves
  },
  data() {
    return {
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        page: 1,
        per_page: 20,
        time: undefined,
        start_time: undefined,
        end_time: undefined
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    datepickerChange(e) {
      const year = new Date(e).getFullYear()
      const month = new Date(e).getMonth() + 1
      const day = new Date(
        new Date(e).getFullYear(),
        new Date(e).getMonth() + 1,
        0
      ).getDate()
      this.listQuery.start_time = year + '-' + month + '-' + '01 00:00:00'
      this.listQuery.end_time = year + '-' + month + '-' + day + ' 23:59:59'
    },
    handleFilter() {
      this.listQuery.page = 1
      this.getList()
    },
    clearFilter() {
      this.listQuery['start_time'] = undefined
      this.listQuery['end_time'] = undefined
      this.listQuery['time'] = undefined
      this.handleFilter()
    },
    getList() {
      this.listLoading = true
      getBusinessTotal(this.listQuery)
        .then((response) => {
          const { data, total } = response.data
          this.list = data
          this.total = total
        })
        .finally(() => {
          this.listLoading = false
        })
    }
  }
}
</script>
